package com.smartEdu.controller.web;

import javax.servlet.http.HttpSession;

import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.commons.Tag_Tips_web;
import com.smartEdu.commons.resp.RespBody;
import com.smartEdu.exception.ControllerException;
import com.smartEdu.vo.web.SysParamVo;

public abstract class WebBaseController{
	
	public final String SYSPARAM = "sysparam";
	private final String baseIframe = "/manager/iframe";

	public WebBaseController(){}
	
	protected String realUrl(String url, Model model) {
		model.addAttribute("p", url);
		return baseIframe;
	}
	
	public final void AssembleResponeBody(Session sess, String[] part, ModelAndView modelAndView) {
		RespBody respBody = new RespBody();
		for (String s : part) {
			respBody.addToMap(s, sess.geto(s));
		}
//		respBody.addToMap(SYSPARAM, sess.geto(SYSPARAM));
		modelAndView.addObject(Tag_Tips.RESPDATA, respBody);
	}
	
	public final void AssembleResponeBody(ModelAndView modelAndView) {
		
	}
	
	public void GetUidFromSession(HttpSession session, Model model) throws ControllerException{
		Object userid = session.getAttribute(Tag_Tips_web.LOGINUSER);
		Object loginname = session.getAttribute(Tag_Tips_web.LOGINAME);
		if((userid != null) && (loginname != null)) {
			SysParamVo sysVo  = new SysParamVo();
			sysVo.setUid(userid.toString());
			sysVo.setLoginname(loginname.toString());
			model.addAttribute(Tag_Tips_web.LOGINUSER, userid);
			model.addAttribute("sysparam", sysVo);
		}
		else {
			throw new ControllerException(Tag_Tips_web.ERR_NOUSER);
		}
	}
	
	public Object GetInfoFromModel(Model model, String key) {
		Object o = model.asMap().get(key);
		return o;
	}
	
}
